package top.lcm889.demo;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author Coder-lcm
 * @date 2021/3/9
 */
public class Demo {
    public static void main(String[] args) {
//        Map<Integer, Integer> map = new HashMap<>();
//        System.out.println(map == null);
//        List<Integer> list = new  LinkedList<Integer>();
//        List<Integer> list2 = new ArrayList<>();
//        Map<String, String> map = new ConcurrentHashMap<String, String>();

        new String();
        new Object();
        List<String> list = Arrays.asList("4234","$24","$24");
        System.out.println(list);
    }

    private static void testCollections() {
        SynchronizedList list = new SynchronizedList();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                String val = null;
                for (int i = 0; i < 100; i++) {
                    val = "t1： " + i;
                    list.add(val);
                    System.out.println("Add: " + val);
                }
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                String val = null;
                for (int i = 0; i < 100; i++) {
                    val = "t2： " +(100 + i);
                    list.add(val);
                    System.out.println("Add: " + val);
                }
            }
        },"t2");

        t1.start();
        t2.start();

        try {
            t1.join(); //等待线程 t1 结束
            t2.join(); //等待线程 t2 结束
        } catch (Exception e) {
        }
        String name = Thread.currentThread().getName();
        System.out.println("Thread name: " + name + ", Obj size: " + list.size());
    }

    static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }
}
class SynchronizedList{
    private List<String> myList = Collections.synchronizedList(new ArrayList<>());
    private Map<String, Integer> map = Collections.synchronizedMap(new HashMap<String, Integer>());
    public void add(String a){
        myList.add(a);
    }

    public void remove(String a){
        myList.remove(a);
    }
    public int size(){
       return myList.size();
    }
}
